using System;
using System.Collections.Generic;
using System.Drawing;
using GameServer.Common.DataStructures2;
using GameServer.Common.Physics;
using GameServer.ShootingGame.Physics.CollisionReactionClasses.Base;
using GameServer.ShootingGame.RenderableClasses;
using GameServer.ShootingGame.MapClasses;

namespace GameServer.ShootingGame.Physics
{


    public static class ShooterPhysics
    {

        public static bool DetectObjectToOrdinanceCollision(GameObject target, Ordinance ordinance)
        {
            double targetRadius;
            double targetCentreX;
            double targetCentreY;
            double ordinanceCollisionPointFX;
            double ordinanceCollisionPointFY;
            RectangleF targetCollidablePart = target.CollidablePart;
            RectangleF ordinanceCollidablePart = ordinance.CollidablePart;
            targetCentreX = target.xPosition + targetCollidablePart.X + targetCollidablePart.Width / 2.0;
            targetCentreY = target.yPosition + targetCollidablePart.Y + targetCollidablePart.Height / 2.0;
            targetRadius = targetCollidablePart.Width / 2.0;
            ordinanceCollisionPointFX = ordinanceCollidablePart.X + ordinanceCollidablePart.Width / 2.0;
            ordinanceCollisionPointFY = ordinanceCollidablePart.Y + ordinanceCollidablePart.Height / 2.0;
            if (CollisionDetection.PointCircleIntersection(targetRadius, targetCentreX, targetCentreY, ordinanceCollisionPointFX, ordinanceCollisionPointFY))
            {

                target.collisionEffects.AddCollision(ordinance);
                ordinance.collisionEffects.AddCollision(target);
                return true;
            }
            return false;
        }

        public static bool DetectAndResolve_OrdinanceToTileCollision(GameObject gameObject)
        {
            return DetectAndResolveObjectTileCollisions(gameObject, Colliders.ORDINANCE);
        }

        public static bool DetectAndResolve_PlayerToTileCollision(GameObject gameObject)
        {
            return DetectAndResolveObjectTileCollisions(gameObject, Colliders.PLAYER);
        }

        public static bool DetectAndResolveObjectTileCollisions(GameObject gameObject, Colliders colliderType)
        {

            List<TileObject> tileObjects = gameObject.GamePlayManager.activeMap.TileObjects;
            //foreach (TileObject tO in tileObjects)
            //{
            //    Console.WriteLine(tO.type);
            //}
            Array2D<int> tileData = gameObject.GamePlayManager.activeMap.TileData;

            GameObject tileObj;
            Object paramObj;
            double tileWidth = gameObject.GamePlayManager.TILE_WIDTH; ;
            double tileHeight = gameObject.GamePlayManager.TILE_HEIGHT;
            RectangleF collidablePart = gameObject.CollidablePart;
            double radius = collidablePart.Width / 2.0;
            double centreX = gameObject.xPosition + collidablePart.Y;
            double centreY = gameObject.yPosition + collidablePart.Y;
            double tileUnits_CoordX = centreX / tileWidth;
            double tileUnits_CoordY = centreY / tileHeight;
            int currentTile_Horizontal_Index = (int)Math.Floor(tileUnits_CoordX);
            int currentTile_Vertical_Index = (int)Math.Floor(tileUnits_CoordY);

            RectangleF tileRect = new RectangleF();
            int i;
            int j;
            int n = 3;
            int m = 3;
            int startIndex_HorizontalLoop;
            int startIndex_VerticalLoop;
            if (currentTile_Horizontal_Index <= 0)
                startIndex_HorizontalLoop = 0;
            else
                startIndex_HorizontalLoop = currentTile_Horizontal_Index - 1;
            if (currentTile_Vertical_Index <= 0)
                startIndex_VerticalLoop = 0;
            else
                startIndex_VerticalLoop = currentTile_Vertical_Index - 1;
            for (i = startIndex_HorizontalLoop; i < startIndex_HorizontalLoop + n; i++)
            {
                for (j = startIndex_VerticalLoop; j < startIndex_VerticalLoop + m; j++)
                {
                    tileRect.X = Convert.ToSingle(i * tileWidth);
                    tileRect.Y = Convert.ToSingle(j * tileHeight);
                    tileRect.Width = Convert.ToSingle(tileWidth);
                    tileRect.Height = Convert.ToSingle(tileHeight);
                    if (colliderType == Colliders.ORDINANCE)
                    {
                        if (CollisionDetection.PointRectangleIntersection(centreX, centreY,
                        tileRect.Left, tileRect.Top, tileRect.Right, tileRect.Bottom))
                        {
                            tileObj = tileObjects[tileData.GetElement(i, j)];
                            gameObject.collisionEffects.AddCollision(tileObj);
                            gameObject.collisionEffects.ResolveCollision();
                        }
                        continue;
                    }
                    if (colliderType == Colliders.PLAYER)
                    {
                        if (i == currentTile_Horizontal_Index && j == currentTile_Vertical_Index)
                            continue;
                        if (CollisionDetection.CircleRectangleIntersection(radius, centreX, centreY,
                        tileRect.Left, tileRect.Top, tileRect.Right, tileRect.Bottom))
                        {
                            tileObj = tileObjects[tileData.GetElement(i, j)];

                            paramObj = tileRect;
                            gameObject.collisionEffects.AddCollision(tileObj, paramObj);
                            gameObject.collisionEffects.ResolveCollision();
                        }
                        continue;
                    }

                }
            }
            return false;
        }

    }
}
